package com.spvs.approval.support.callBack;

import com.spvs.approval.domain.ApprovalFromEntity;
import com.spvs.approval.support.ApprovalBusinessCall;
import com.spvs.approval.support.ApprovalBusinessContext;
import com.spvs.system.api.IPurchaseService;
import com.spvs.system.api.domain.PurchaseCallBackAPI;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 安装验收审批
 */
@Component
@Slf4j
public class AZYSBusinessCallBack implements ApprovalBusinessCall {

    private static final long PURCHASE_TYPE_X = 3L;

    @Autowired
    private IPurchaseService purchaseService;

    @Override
    public Boolean isCall(String type) {
        return ApprovalBusinessContext.TYPE_AZYS.equals(type);
    }

    @Override
    public void callback(ApprovalFromEntity from, Integer userSort,Long userId) {
        if (from != null) {
            // 校验from对象的其他属性，如businessId和status的合法性...
            if (from.getStatus() == null || from.getBusinessId() == null) {
                log.error("表单内容为空");
            }else{
                PurchaseCallBackAPI purchaseCallBackAPI = new PurchaseCallBackAPI();
                purchaseCallBackAPI.setId(from.getBusinessId());
                purchaseCallBackAPI.setPurchaseType(PURCHASE_TYPE_X);
                Integer state = 0;
                switch (from.getStatus()){
                    case 101:
                        state = 1;
                        break;
                    case 201:
                        state = 2;
                        break;
                    case 301:
                        state = 3;
                        break;
                    case 302:
                        state = 4;
                        break;
                }
                purchaseCallBackAPI.setAuditStatus(state);
                try {
                    // 异常处理增强
                    purchaseService.callBack(purchaseCallBackAPI);
                } catch (Exception e) {
                    // 记录日志，以便于问题追踪和定位
                    log.error("安装验收审批回调出错: " + e.getMessage(), e);
                }
            }
        }
        log.error("审批表单为空");
    }
}
